package com.srs.dao.info;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.srs.model.info.GrxQualityPercent;
import com.srs.util.HibernateUtil;

public class GrxQualityPercentDao {

	final static Logger logger = LoggerFactory.getLogger(GrxQualityPercentDao.class);

	private List<GrxQualityPercent> grxQualityPercentList;
	private GrxQualityPercent grxQualityPercent;

	public void delete(Integer id) {

		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			grxQualityPercent = (GrxQualityPercent) session.get(GrxQualityPercent.class, id);
			session.delete(grxQualityPercent);
			tx.commit();

		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<GrxQualityPercent> list() {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			grxQualityPercentList = session.createQuery("from User").list();
			return grxQualityPercentList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}


	@SuppressWarnings("unchecked")
	public List<GrxQualityPercent> list(String onDate) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("from GrxQualityPercent gqp where gqp.onDate=:onDate");
			q.setString("onDate", onDate);
			grxQualityPercentList = q.list();
			return grxQualityPercentList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}			
	
	@SuppressWarnings("unchecked")
	public List<GrxQualityPercent> list(String onDate, String agentList) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("from GrxQualityPercent gqp where gqp.onDate=:onDate and gqp.agentId in " + agentList );
			q.setString("onDate", onDate);
			grxQualityPercentList = q.list();
			return grxQualityPercentList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}		
	
	@SuppressWarnings("unchecked")
	public List<GrxQualityPercent> list(String onDate, Integer cellId) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("from GrxQualityPercent gqp where gqp.onDate=:onDate and gqp.cellId=:cellId");
			q.setString("onDate", onDate);
			q.setInteger("cellId", cellId);
			grxQualityPercentList = q.list();
			return grxQualityPercentList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}				
	
	public GrxQualityPercent get(Integer id) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from User u where u.uid=:id");
			q.setInteger("userid", id);
			return (GrxQualityPercent) q.uniqueResult();
		} finally {
			session.close();
		}

	}

	public void insert(GrxQualityPercent usr) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(usr);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

	public void update(GrxQualityPercent usr) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(usr);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

}
